{layout name="layout/header" /}
<link href="root/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="root/css/plugins/iCheck/custom.css" rel="stylesheet">

<link href="root/css/plugins/switchery/switchery.css" rel="stylesheet">
<script src="root/js/plugins/switchery/switchery.js"></script>
<!--<link href="root/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">-->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5><a href="{:url('vote/index')}">活动列表</a>/{$vote.name} 白名单</h5>
                    </div>
                    <div class="ibox-content">
                        <p>
                            <a type="button" class="btn btn-outline btn-success" data-toggle="modal" href="#modal-form">新增</a>
                            <!--<a type="button" class="btn btn-outline btn-success" href="{:url('addWhite',['vote_id'=>$vote.id])}">新增</a>-->
                            <button type="button" class="btn btn-primary btn-outline refresh">刷新</button>
                            <a type="button" class="btn btn-primary btn-outline" href="{:url('vote/index')}">返回列表</a>
                        </p>
                    </div>
                </div>
                <div class="ibox-content">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>手机号码</th>
                                <th>限制投票数(每天/每周)</th>
                                <th>每个作品限制投票数(0:不限制)</th>
                                <th>点击投票数</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        {volist name="white" id="vo"}
                        <tr pid="{$vo.id}" >
<!--                            <td><input type="text" class="form-control update_text" data-field="mobile" value="{$vo.mobile}"></td>-->
<!--                            <td><input type="text" class="form-control update_text" data-field="nums" value="{$vo.nums}"></td>-->
<!--                            <td><input type="text" class="form-control update_text" data-field="per_nums" value="{$vo.per_nums}"></td>-->
<!--                            <td><input type="text" class="form-control update_text" data-field="click_nums" value="{$vo.click_nums}"></td>-->
                            <td>{$vo.mobile}</td>
                            <td>{$vo.nums}</td>
                            <td>{$vo.per_nums}</td>
                            <td>{$vo.click_nums}</td>
                            <td>{$vo.addtime|date="Y-m-d H:i",###}</td>
                            <td>
                                <button
                                        type="button"
                                        class="btn btn-primary btn-sm edit"
                                        data-white_id="{$vo.id}"
                                        data-mobile="{$vo.mobile}"
                                        data-nums="{$vo.nums}"
                                        data-per_nums="{$vo.per_nums}"
                                        data-click_nums="{$vo.click_nums}"
                                        data-toggle="modal"
                                        href="#modal-form"
                                >编辑</button>
                                <button type="button" class="btn btn-danger btn-sm del" >删除</button>
                            </td>
                        </tr>
                        {/volist}
                        </tbody>
                        <tfoot>
                        <tr>
                            <td colspan="6">

                            </td>
                        </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
    <!--新添弹窗-->
    <div id="modal-form" class="modal fade" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-sm-12 b-r">
                            <h3 class="m-t-none m-b">新增</h3>
                            <form id="form">
                                <input type="hidden" id="white_id" name="white_id" />
                                <div class="form-group">
                                    <label>手机号码</label>
                                    <input type="text" placeholder="手机号码" id="mobile" name="mobile" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label>类型限制投票总数</label>
                                    <input type="text" placeholder="(每天/每周)投票的数量" id="nums" name="nums" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label>每作品投票数(默认0，不限制)</label>
                                    <input type="text" placeholder="默认0，不限制" id="per_nums" name="per_nums" value="0" class="form-control">
                                </div>
                                <div class="form-group">
                                    <label>每次点击投票数</label>
                                    <input type="text" placeholder="每次点击投票数" id="click_nums" name="click_nums" value="1" class="form-control">
                                </div>
                                <!--<div class="form-group">-->
                                <!--<label >前台显示</label>-->
                                <!--<select class="form-control m-b" id="display" name="display">-->
                                <!--<option value="1">显示</option>-->
                                <!--<option value="0">隐藏</option>-->
                                <!--</select>-->
                                <!--</div>-->
                                <div class="form-group">
                                    <div class="col-sm-4 col-sm-offset-2">
                                        <button class="btn btn-primary" id="sub" data-dismiss="modal">提交</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--新添结束-->
<!--layer-->
<script src="root/js/plugins/layer/layer.min.js"></script>
<!-- iCheck -->
<script src="root/js/plugins/iCheck/icheck.min.js"></script>
<script>
    var id= "{$vote.id}";
    $(document).ready(function() {
        $('#modal-form').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget);
            if (!button.hasClass('edit')) {
                $('#white_id').val('');
                return 0;
            }
            var white_id=button.data('white_id');
            var mobile=button.data('mobile');
            var nums=button.data('nums');
            var per_nums=button.data('per_nums');
            var click_nums=button.data('click_nums');
            $("#white_id").val(white_id);
            $("#mobile").val(mobile);
            $("#nums").val(nums);
            $("#per_nums").val(per_nums);
            $("#click_nums").val(click_nums);
        });
        $('#modal-form').on('hidden.bs.modal', function (event) {
            if ($("#white_id").val() !== '') {
                $("#white_id").val('');
                $("#mobile").val('');
                $("#nums").val('');
                $("#per_nums").val('');
                $("#click_nums").val('');
            }
        });
        checks();
        //单选，复选按钮
        function checks () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green'
            })
        };
        var elem = document.querySelectorAll('.js-switch');
        elem.forEach(function(i,index){
            var a= "a" +index;
            a = new Switchery(i, {
            color: '#1AB394'
            });
        });
        //新添扩展字段
        $('#sub').click(function () {
            var white_id = $('#white_id').val();
            var mobile = $('#mobile').val().trim();
            if(mobile == '' ){
                layer.msg('电话不能为空!');
                return false;
            }
            if(!(/^1\d{10}$/).test(mobile)){
                layer.msg('手机号码格式不正确')
                return false;
            }
            var nums = $("#nums").val().trim();
            var per_nums = $("#per_nums").val().trim();
            var click_nums = $("#click_nums").val().trim();
            var reg = /^\d+$/;
            if(!reg.test(nums) || !reg.test(per_nums) || !reg.test(click_nums)){
                layer.msg('数字格式不正确')
                return false;
            }
            if(nums <= 0 || click_nums <=0){
                layer.msg('投票总数或点击投票数不能小于1');
                return false;
            }
            var api = "";
            if (white_id !== "") {
                api = "{:url('ajaxWhiteUpdate')}";
            } else {
                api = "{:url('ajaxAddWhite')}";
            }
            $.ajax({
                url: api,
                type:"post",
                data:{id:id,mobile:mobile,nums:nums,per_nums:per_nums,click_nums:click_nums, white_id: white_id},
                dataType:"json",
                success:function (data) {
                    if(data.code == 1){
                        layer.msg(data.msg);
                        setTimeout(function(){
                            window.location.reload();
                        },1500);
                        return false;
                    }else {
                        layer.msg(data.msg);
                        return false;
                    }
                }
            });
        });

        //列表页修改
        $(".update_checkbox").change(function () {
            var field = $(this).attr('data-field');
            var value = $(this).is(':checked')?1:0;
            var id = $(this).parent().parent().attr('pid');
            update(id,field,value);
        });
        //列表页修改
        $(".update_text").blur(function () {
            var field = $(this).attr('data-field');
            var id = $(this).parent().parent().attr('pid');
            var value = $(this).val().trim();
            update(id,field,value);
        });
        function update(id,field,value) {
            $.ajax({
                url:"{:url('ajaxUpdateWhite')}",
                type:'post',
                dataType:'json',
                data:{field:field,value:value,id:id},
                success:function (data) {
                    console.log(data.msg);
                    if(data.code == 0){
                        layer.msg(data.msg);
                    }
                }
            })
        }
        //删除
        $('.del').click(function(){
            var id = $(this).parent().parent().attr('pid');
            var $this = $(this);

            layer.confirm('确定删掉吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    url:"{:url('ajaxDelWhite')}",
                    type:'post',
                    data:{id:id},
                    dataType:'json',
                    success:function (data) {
                        layer.msg(data.msg);
                        if(data.code ==1){
                            $this.parent().parent().remove();
                        }
                    }
                })
            }, function(){

            });

        });

    });
</script>
